﻿using UnityEngine;
using System.Collections;

public class StateManager : MonoBehaviour 
{
	public static StateManager Instance;

	public enum GAME_STATES
	{
		MAIN_MENU,
		CREDITS,
		STORYBOARD,
		INGAME,
		GAME_OVER,
		ACHIEVEMENTS,
		SHOP,

		PAUSE,
		LOADING_SCREEN,

		DEFAULT
	};

	public GAME_STATES currentState = GAME_STATES.DEFAULT;
	private GAME_STATES tempState = GAME_STATES.DEFAULT;

	public delegate void GameState();
	public static event GameState OnMainMenuScreen;
	public static event GameState OnStoryboardScreen;
	public static event GameState OnInGameScreen;
	public static event GameState OnGameOverScreen;
	public static event GameState OnAchievementsScreen;
	public static event GameState OnShopScreen;
	
	public static event GameState OnCreditsScreen;
	public static event GameState OnPauseScreen;
	public static event GameState OnLoadingScreen;
	
	[SerializeField] Transform loadingScreenTop;
	[SerializeField] Transform loadingScreenBottom;
	
	[SerializeField] Transform pauseButton;
	[SerializeField] Transform pauseScreen;
	[SerializeField] Transform creditsScreen;
	
	public delegate void SoundState();
	public static event SoundState OnBGMEnable;
	public static event SoundState OnBGMDisable;
	public static event SoundState OnSFXEnable;
	public static event SoundState OnSFXDisable;

	[SerializeField] Transform bgmButton;
	[SerializeField] Transform sfxButton;

	void Awake () 
	{
		Instance = this;
		currentState = GAME_STATES.MAIN_MENU;

		CheckBGMandSFX ();
	}

	void FixedUpdate () 
	{
		switch ( currentState )
		{
		case GAME_STATES.MAIN_MENU:
			if(OnMainMenuScreen != null)
				OnMainMenuScreen ();
			break;

		case GAME_STATES.STORYBOARD:
			if(OnStoryboardScreen != null)
				OnStoryboardScreen ();
			break;

		case GAME_STATES.INGAME:
			if(OnInGameScreen != null)
				OnInGameScreen ();
			break;

		case GAME_STATES.GAME_OVER:
			if(OnGameOverScreen != null)
				OnGameOverScreen ();
			break;

		case GAME_STATES.ACHIEVEMENTS:
			if(OnAchievementsScreen != null)
				OnAchievementsScreen ();
			break;

		case GAME_STATES.SHOP:
			if(OnShopScreen != null)
				OnShopScreen ();
			break;

		case GAME_STATES.LOADING_SCREEN:
			if(OnLoadingScreen != null)
				OnLoadingScreen ();
			break;

		case GAME_STATES.PAUSE:
			if(OnPauseScreen != null)
				OnPauseScreen ();
			break;

		case GAME_STATES.CREDITS:
			if(OnCreditsScreen != null)
				OnCreditsScreen ();
			break;
		}
	}

	public void CallCreditsScreen ()
	{
		if ( !creditsScreen.gameObject.activeSelf )
		{
			tempState = currentState;
			currentState = GAME_STATES.CREDITS;
			creditsScreen.gameObject.SetActive(true);
		}
		else
		{
			currentState = tempState;
			creditsScreen.gameObject.SetActive(false);
		}
	}

	public void CallPauseScreen ()
	{
		if ( !pauseScreen.gameObject.activeSelf )
		{
			tempState = currentState;
			currentState = GAME_STATES.PAUSE;
			pauseScreen.gameObject.SetActive(true);
		}
		else
		{
			currentState = tempState;
			pauseScreen.gameObject.SetActive(false);
		}
	}

	public void CallLoadingScreen ( GAME_STATES nextState )
	{
		currentState = GAME_STATES.LOADING_SCREEN;
		StartCoroutine ( LoadingScreen ( nextState ) );
	}

	IEnumerator LoadingScreen ( GAME_STATES nextState )
	{
		float fromValue = 410;
		float toValue = 132;

		TweenPosition topTween = loadingScreenTop.GetComponent<TweenPosition>();
		TweenPosition bottomTween = loadingScreenBottom.GetComponent<TweenPosition>();

		topTween.from = Vector3.up * fromValue;
		topTween.to = Vector3.up * toValue;

		bottomTween.from = Vector3.up * -fromValue;
		bottomTween.to = Vector3.up * -toValue;

		topTween.method = UITweener.Method.BounceIn;
		bottomTween.method = UITweener.Method.BounceIn;

		topTween.Reset();
		bottomTween.Reset();

		topTween.enabled = true;
		bottomTween.enabled = true;

		yield return new WaitForSeconds ( 1.5f );

		topTween.from = Vector3.up * toValue;
		topTween.to = Vector3.up * fromValue;
		
		bottomTween.from = Vector3.up * -toValue;
		bottomTween.to = Vector3.up * -fromValue;
		
		topTween.method = UITweener.Method.Linear;
		bottomTween.method = UITweener.Method.Linear;

		//Change the current state
		currentState = nextState;

		topTween.Reset();
		bottomTween.Reset();
		
		topTween.enabled = true;
		bottomTween.enabled = true;
		
		yield return null;
	}

	void CheckBGMandSFX ()
	{
		if ( PlayerPrefs.GetInt("GAME_BGM") == 0 )
			bgmButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_MusicOn";
		else
			bgmButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_MusicOff";

		if ( PlayerPrefs.GetInt("GAME_SFX") == 0 )
			sfxButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_SoundOn";
		else
			sfxButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_SoundOff";
	}

	void EnableBGM ()
	{
		if ( currentState == GAME_STATES.LOADING_SCREEN || currentState == GAME_STATES.PAUSE || currentState == GAME_STATES.CREDITS )
			return;

		if ( PlayerPrefs.GetInt("GAME_BGM") == 0 )
		{
			bgmButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_MusicOff";
			PlayerPrefs.SetInt("GAME_BGM", 1);
		}
		else
		{
			bgmButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_MusicOn";
			PlayerPrefs.SetInt("GAME_BGM", 0);
		}
	}

	void EnableSFX ()
	{
		if ( currentState == GAME_STATES.LOADING_SCREEN || currentState == GAME_STATES.PAUSE || currentState == GAME_STATES.CREDITS )
			return;

		if ( PlayerPrefs.GetInt("GAME_SFX") == 0 )
		{
			sfxButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_SoundOff";
			PlayerPrefs.SetInt("GAME_SFX", 1);
		}
		else
		{
			sfxButton.GetComponent<UIButton>().tweenTarget.GetComponent<UISlicedSprite>().spriteName = "Button_SoundOn";
			PlayerPrefs.SetInt("GAME_SFX", 0);
		}
	}

	void OnGUI ()
	{
		if(GUI.Button(new Rect(Screen.width / 2 - 75, 5, 150, 30), "Show GameOver"))
		{
			CallLoadingScreen ( GAME_STATES.GAME_OVER );
		}

	}
}
